using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class BPCEMVisualisation : MonoBehaviour
{
	public Vector3 center;

	public Vector3 size;

	public Vector3 position;

	public bool useLocalValues;

	public ReflectionProbe reflectionProbe;

	public bool setParamsFromReflectionProbe;

	public bool createGameObjectsListFromChildren;

	public bool usePosition;

	public List<GameObject> gameObjects = new List<GameObject>();

	private const string _BBoxMin = "_BBoxMin";

	private const string _BBoxMax = "_BBoxMax";

	private const string _EnviCubeMapPos = "_EnviCubeMapPos";

	private void OnDrawGizmos()
	{
		if (!base.enabled)
		{
			return;
		}
		Renderer component = GetComponent<Renderer>();
		if (!(component == null))
		{
			Material sharedMaterial = component.sharedMaterial;
			if (!(sharedMaterial == null))
			{
				Color color = Gizmos.color;
				Gizmos.color = Color.green;
				Vector3 vector = GetPosition(center);
				Vector3 vector2 = GetPosition(center + size) - vector;
				Gizmos.DrawWireCube(vector, vector2);
				Gizmos.color = color;
			}
		}
	}

	private Vector3 WorldToLocal(Vector3 pos)
	{
		return base.transform.InverseTransformPoint(pos);
	}

	private Vector3 GetPositionToSet(Vector3 pos)
	{
		if (useLocalValues)
		{
			return WorldToLocal(pos);
		}
		return pos;
	}

	private Vector3 GetPosition(Vector3 pos)
	{
		if (useLocalValues)
		{
			return base.transform.TransformPoint(pos);
		}
		return pos;
	}

	private void SetParamsFromReflectionProbe()
	{
		if (!(reflectionProbe == null))
		{
			center = GetPositionToSet(reflectionProbe.bounds.center);
			size = 2f * (GetPositionToSet(reflectionProbe.bounds.max) - center);
			position = GetPositionToSet(reflectionProbe.transform.position);
		}
	}

	private void CreateGameObjectsListFromChildren()
	{
		gameObjects.Clear();
		CreateGameObjectsListFromChildren(base.transform);
	}

	private void CreateGameObjectsListFromChildren(Transform transform)
	{
		foreach (Transform item in transform)
		{
			if (item.GetComponent<BPCEMVisualisation>() == null)
			{
				CreateGameObjectsListFromChildren(item);
			}
			GameObject gameObject = item.gameObject;
			Renderer component = gameObject.GetComponent<Renderer>();
			if (!(component == null))
			{
				Material sharedMaterial = component.sharedMaterial;
				if (!(sharedMaterial == null) && sharedMaterial.HasProperty("_BBoxMin") && sharedMaterial.HasProperty("_BBoxMax") && sharedMaterial.HasProperty("_EnviCubeMapPos"))
				{
					gameObjects.Add(gameObject);
				}
			}
		}
	}

	private void Update()
	{
		if (setParamsFromReflectionProbe)
		{
			SetParamsFromReflectionProbe();
		}
		Vector3 vector = GetPosition(center);
		Vector3 vector2 = GetPosition(center + size) - vector;
		Vector3 vector3 = GetPosition(position);
		if (!usePosition)
		{
			vector3 = vector;
		}
		Vector3 vector4 = vector - vector2 / 2f;
		Vector3 vector5 = vector + vector2 / 2f;
		if (Application.isEditor && createGameObjectsListFromChildren)
		{
			CreateGameObjectsListFromChildren();
		}
		List<GameObject> list = gameObjects;
		for (int i = 0; i < list.Count; i++)
		{
			GameObject gameObject = list[i];
			if (gameObject == null)
			{
				continue;
			}
			Renderer component = gameObject.GetComponent<Renderer>();
			if (!(component == null))
			{
				Material sharedMaterial = component.sharedMaterial;
				if (!(sharedMaterial == null))
				{
					sharedMaterial.SetVector("_BBoxMin", vector4);
					sharedMaterial.SetVector("_BBoxMax", vector5);
					sharedMaterial.SetVector("_EnviCubeMapPos", vector3);
				}
			}
		}
	}
}
